package com.tjsinfo.tjsoa.mail.bo;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Expression;

import com.tjsinfo.tjsoa.Dao.QbcDao;
import com.tjsinfo.tjsoa.mail.dao.ITjsEmailFolderDao;
import com.tjsinfo.tjsoa.mail.vo.TjsEmailFolder;

import com.tjsinfo.tjsoa.system.vo.TSystemUser;

public class TjsEmailFolderBo implements ITjsEmailFolderBo{
	private final boolean STARTTRUE=true;
	private final boolean STARTFALSE=false;
	private ITjsEmailFolderDao tjsEmailFolderDao;
	private QbcDao qbcDao;
	public QbcDao getQbcDao() {
		return qbcDao;
	}

	public void setQbcDao(QbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}

	public ITjsEmailFolderDao getTjsEmailFolderDao() {
		return tjsEmailFolderDao;
	}

	public void setTjsEmailFolderDao(ITjsEmailFolderDao tjsEmailFolderDao) {
		this.tjsEmailFolderDao = tjsEmailFolderDao;
	}

	public String addTjsEmailFold(String foldername, Integer userid) {
		// TODO Auto-generated method stub
		try {
			TjsEmailFolder tjsEmailFolder = new TjsEmailFolder();//生成一个文件夹
			tjsEmailFolder.setFolderName(foldername);
			TSystemUser tjsUser = new TSystemUser();
			tjsUser.setUserId(userid);
			tjsEmailFolder.setTjsUser(tjsUser);
			return  tjsEmailFolderDao.addFlod(tjsEmailFolder);
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
		
	}
	/**
	 * 删除邮件文件夹
	 */
	public boolean deleteTjsEmailFold(String foldid) {
		// TODO Auto-generated method stub
		try {
			TjsEmailFolder emailFolder = selectOneByID(foldid);//生成一个文件夹
			tjsEmailFolderDao.delete(emailFolder);
			return STARTTRUE;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTFALSE;
		}
		
	}
	/**
	 * 更新邮件名称
	 * 
	 */
	public boolean updateTjsEmailFold(String foldername, String foldid) {
		// TODO Auto-generated method stub
		try {
			TjsEmailFolder tjsEmailFolder = selectOneByID(foldid);//生成一个文件夹
			tjsEmailFolder.setFolderName(foldername);
			tjsEmailFolderDao.update(tjsEmailFolder);
			return STARTTRUE;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTFALSE;
		}
		
	}
	/**
	 * 查询一个文件夹
	 * @param folderid
	 * @return
	 */
	private TjsEmailFolder selectOneByID(String folderid)
	{
	   TjsEmailFolder tjsEmailFolder =	 tjsEmailFolderDao.selectOneById(folderid);//生成一个文件夹
	   return tjsEmailFolder;
	}
	
	/**
	 * 根据用户ID查询文件夹
	 */
	public List<TjsEmailFolder> selectByUserId(Integer userid) {
		// TODO Auto-generated method stub
		DetachedCriteria criteria = DetachedCriteria.forClass(TjsEmailFolder.class);
		criteria.add(Expression.eq("tjsUser.id", userid));
		return 	qbcDao.getCriteria(criteria);
	}

	public boolean deleteTjsEmailFoldList(String[] array) {
		// TODO Auto-generated method stub
		try {
			for (int i = 0; i < array.length; i++) {
				deleteTjsEmailFold(array[i]);
			}
			return STARTTRUE;
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTFALSE;
		}
		
	}
	
	/**
	 * 查询一个
	 * @param id
	 * @return
	 */
	public TjsEmailFolder selectOneId(String id)
	{
		return  tjsEmailFolderDao.selectOneById(id);
	}
}
